﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class CostManage_MeterRpt : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AttributeBind();
            Dep_Bind();
            Auth();
            Report_Bind();
            SetPager();
        }
    }

    private void AttributeBind()
    {
        Month_A.Text = DateTime.Now.AddMonths(-1).ToString("yyyy年MM月");
        Month_A.Attributes.Add("onclick", "WdatePicker({dateFmt:'yyyy年MM月',maxDate:'%y年#{%M-1}月',minDate:'2014年11月'});");
    }

    private void Dep_Bind()
    {
        string sqlstr = "SELECT DISTINCT SUBSTR(Replace(CELL_CHNAME,'G',''),0,2) AS County FROM CELL_CLASS ORDER BY SUBSTR(Replace(CELL_CHNAME,'G',''),0,2)";
        Dep_A.DataSource = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null).Tables[0].DefaultView;
        Dep_A.DataTextField = "County";
        Dep_A.DataValueField = "County";
        Dep_A.DataBind();
        Dep_A.Items.Insert(0, new ListItem("全市", "0"));
    }

    private void Auth()
    {
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            Dep_A.SelectedValue = dr["company"].ToString();
        }
        else
        {
            Dep_A.Enabled = true;
        }
    }

    private void Report_Bind()
    {
        string sqlStr = "AB_MeterReport_Pk.GetMeterRpt";
        OracleParameter[] _params = {
                new OracleParameter("v_Month",OracleType.VarChar,6),
				new OracleParameter("V_Organ",OracleType.VarChar,4),	
                new OracleParameter("V_Way",OracleType.VarChar,6),	
				new OracleParameter("resultSet",OracleType.Cursor)
            };
        _params[0].Value = Month_A.Text.Replace("年", "").Replace("月", "");
        _params[1].Value = Dep_A.SelectedValue == "0" ? "" : Dep_A.SelectedValue;
        _params[2].Value = MeterWay_A.SelectedValue;
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            _params[1].Value = dr["company"].ToString();
        }
        _params[3].Direction = ParameterDirection.Output;
        DataSet ds = OracleHelper.ExecuteDataSet(sqlStr, CommandType.StoredProcedure, _params);
        Report.DataSource = ds;
        Report.DataBind();
    }

    protected void Query_Click(object sender, EventArgs e)
    {
        Report_Bind();
        SetPager();
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetMeterDetail();
        DetailPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = DetailPager.PageSize;
        pds.CurrentPageIndex = DetailPager.CurrentPageIndex - 1;
        MeterDetail.DataSource = pds;
        MeterDetail.DataBind();
    }

    private DataSet GetMeterDetail()
    {
        string sqlStr = "AB_MeterReport_Pk.GetMeterDetail";
        OracleParameter[] _params = {
                new OracleParameter("v_Month",OracleType.VarChar,6),
				new OracleParameter("V_Organ",OracleType.VarChar,4),	
                new OracleParameter("V_Way",OracleType.VarChar,6),	
				new OracleParameter("resultSet",OracleType.Cursor)
            };
        _params[0].Value = Month_A.Text.Replace("年", "").Replace("月", "");
        _params[1].Value = Dep_A.SelectedValue == "0" ? "" : Dep_A.SelectedValue;
        _params[2].Value = MeterWay_A.SelectedValue;
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            _params[1].Value = dr["company"].ToString();
        }
        _params[3].Direction = ParameterDirection.Output;
        DataSet ds = OracleHelper.ExecuteDataSet(sqlStr, CommandType.StoredProcedure, _params);
        return ds;
    }

    protected void DetailPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }
}
